"use strict";
const dbTool = require("../utils/db");
const SuiteToOrg = require("mongoose").model("SuiteToOrg");

const updateOrgTempCodeBySuiteKey = function(suiteKey, tempAuthCode) {
	const condition = { //temp codeh和percode都是跟org走的，每个org都不同，因为是购买记录什么的
		suiteKey
	};
	Object.assign(condition, this);
	const update = {
		$set: {
			tempAuthCode,
			suiteKey,
			domainId: this.domainId,
			orgId: this.orgId
		}
	};
	const option = {
		upsert: true
	};
	console.log("更新temp code参数是", condition);
	return SuiteToOrg.findOneAndUpdate(condition, update, option)
		.exec()
		.catch(e => console.error(e));
};
const updatePermanentCodeBySuiteKey = function(suiteKey, permanentCode) {
	const condition = {
		suiteKey
	};
	Object.assign(condition, this);
	const update = {
		$set: {
			permanentCode
		}
	};
	console.log("更新permanent code参数是", condition);
	return SuiteToOrg.findOneAndUpdate(condition, update)
		.exec()
		.catch(e => console.error(e));
};
const getSuiteToOrgBySuiteKey = function(suiteKey) {
	const condition = Object.assign({
		suiteKey
	}, this);
	const projection = {
		_id: 0,
		__v: 0
	};
	console.log("查询购买关系条件是", condition);
	return SuiteToOrg.findOne(condition, projection)
		.populate("suiteKey", projection)
		.exec()
		.then(dbTool.toSimpleObject);

};
module.exports = {
	updateOrgTempCodeBySuiteKey,
	updatePermanentCodeBySuiteKey,
	getSuiteToOrgBySuiteKey
};